<?php
namespace app\farm\model;

use app\banquet\model\Place;
use app\BaseModel;
use think\facade\Db;

class Evaluate extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_evaluate_list';


    protected $append = [

        'order_info'
    ];


    /**
     * @author chenniang
     * @DataTime: 2022-02-10 17:36
     * @功能说明:
     */
    public function getOrderInfoAttr($value,$data){

        if(isset($data['type'])&&isset($data['order_id'])&&isset($data['farmer_id'])){

            $model = $this->getTypeModel($data['type'],2);

            $info  = $model->dataInfo(['id'=>$data['order_id']]);

            $farmer_model = new \app\farm\model\Farmer();

            $info['farmer_info'] = $farmer_model->dataInfo(['id'=>$data['farmer_id']],'title');

            return $info;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 17:14
     * @功能说明:转换图片格式
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);

        }else{

            return [];
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-10 17:37
     * @功能说明:根据type获取模型
     */
    public function getTypeModel($type,$is_order=1){

        switch ($type){

            case 1:

                $model = new ClaimOrder();

                break;

            case 2:

                $model = new LandOrder();

                break;

            case 3:

                $model = $is_order ==1?new ShopOrderGoods():new ShopOrder();

                break;

            default:

                $model = new ClaimOrder();

                break;

        }

        return $model;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-10 13:33
     * @功能说明:评价管理
     */
    public function adminList($dis,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_farmer b','a.farmer_id = b.id','left')
                ->join('lbfarm_user_list c','a.user_id = c.id','left')
                ->where($dis)
                ->field('a.*,b.title,c.nickName,c.avatarUrl')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-11 10:04
     * @功能说明:根据type获取key
     */
    public function getObjKey($type){

        switch ($type){

            case 1:

                $key = 'claim_id';

                break;
            case 2:

                $key = 'land_id';

                break;

            case 3:

                $key = 'goods_id';

                break;
            default:
                $key = 'claim_id';

                break;
        }

        return $key;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-11 09:53
     * @功能说明:评价列表
     */
    public function goodsEvaluateList($goods_id,$type,$is_goods=0){

        $order_model = $this->getTypeModel($type);

        $key = $this->getObjKey($type);

        $dis = [

          //  'pay_type' => 7,

            $key => $goods_id
        ];

        if($type==3){

            $order_id = $order_model->where($dis)->column('order_id');

        }else{

            $order_id = $order_model->where($dis)->column('id');
        }

        $where[] = ['status','>',-1];

        $where[] = ['order_id','in',$order_id];

        $where[] = ['type','=',$type];

        $dis_where = $where;

        if($is_goods==1){

            $where[] = ['star','>=',3];

        }

        if($is_goods==2){

            $where[] = ['star','<',3];

        }

        $data = $this->dataList($where);

        if(!empty($data['data'])){

            $user_model = new User();

            foreach ($data['data'] as &$v){

                $v['user_info'] = $user_model->where(['id'=>$v['user_id']])->field('nickName,avatarUrl')->find();

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }

        }

        $data['good_count'] = $this->where($dis_where)->where('star','>=',3)->count();

        $data['bad_count']  = $this->where($dis_where)->where('star','<',3)->count();

        $data['all_count']  = $this->where($dis_where)->count();

        return $data;

    }



    /**
     * @param $place_id
     * @功能说明:修改场地的评分
     * @author chenniang
     * @DataTime: 2022-01-11 15:49
     */
    public function updateFarmerStar($farmer_id){

        $dis[] = ['farmer_id','=',$farmer_id];

        $dis[] = ['status','>',-1];

        $find  = $this->where($dis)->find();

        $star  = $this->where($dis)->avg('star');

        $star  = !empty($find)?$star:5;

        $farmr_model = new Farmer();

        $res = $farmr_model->dataUpdate(['id'=>$farmer_id],['star'=>$star]);

        return $res;

    }






}